<!-- 新增和编辑弹窗 -->
<template>
  <a-modal
    :width='800'
    :visible='visible'
    :confirm-loading='loading'
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
    <a-form
      layout='horizontal'
      ref='form'
      :model='form'
      :rules='rules'
      :label-col='{ md: { span: 4 }, sm: { span: 24 } }'
      :wrapper-col='{ md: { span: 20 }, sm: { span: 24 } }'
    >
      <a-form-item label='车辆名字:' name='vehicleName'>
        <a-input v-model:value='form.vehicleName' placeholder='请输入车辆名字' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='车牌号:' name='license'>
        <a-input v-model:value='form.license' placeholder='请输入车牌号' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='载客数:' name='passenger'>
        <a-input-number v-model:value='form.passenger' placeholder='请输入载客数' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='车辆品牌:' name='brand' >
        <a-input  v-model:value='form.brand' placeholder='请输入车辆品牌' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='车辆型号:' name='vehicleType' >
        <a-input  v-model:value='form.vehicleType' placeholder='请输入车辆型号' allow-clear autocomplete='off' />
      </a-form-item>

      <a-form-item label="车辆类型/用途:" name="modeId">
        <a-select
          show-search
          v-model:value="form.modeId"
          placeholder="请选择所属车辆类型/用途"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
          allow-clear
        >
          <a-select-option v-for="item in usemodels" :key="item.id" >
            {{ item.modeName }}
          </a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="车辆燃油种类:" name="fuelType">
        <a-select
          show-search
          v-model:value="form.fuelType"
          placeholder="请选择所属燃油种类"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
          allow-clear
        >
          <a-select-option v-for="item in fuletypes" :key="item.id" >
            {{ item.fuelName }}
          </a-select-option>
        </a-select>
      </a-form-item>

    </a-form>
  </a-modal>
</template>

<script>
import { VehicleApi } from '@/api/property/vechile/VehicleApi';
import { message } from 'ant-design-vue';
import {VehicleModeApi} from "@/api/property/vechile/VehicleModeApi";
import {VehicleFueltypesApi} from "@/api/property/vechile/VehicleFueltypesApi";

export default {
  name: 'VehicleManagerVehicleEdit',
  components: {
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  created() {
    console.log(this.data);
    if (this.data) {
      this.form = Object.assign({}, this.data);
      this.isUpdate = true;
    } else {
      this.form = {};
      this.isUpdate = false;
    }
    if (this.$refs.form) {
      this.$refs.form.clearValidate();
    }
  },
  async mounted() {
    VehicleModeApi.list({}).then(resp => {
      if (resp && resp.data &&  resp.data.length > 0) {
        this.usemodels = resp.data;
      }
    });

    VehicleFueltypesApi.list({}).then(resp => {
      if (resp && resp.data &&  resp.data.length > 0) {
        this.fuletypes = resp.data;
      }
    });
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        vehicleName: [{ required: true, message: '请输入车辆名字', type: 'string', trigger: 'blur' }],
        license: [{ required: true, message: '请输入车牌号', type: 'string', trigger: 'blur' }],
        passenger: [{ required: true, message: '请输入载客数', type: 'number', trigger: 'blur' }],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      usemodels : [],
      fuletypes : []
    };
  },
  methods: {
    /**
     * 保存和编辑
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    async save() {
      console.log(this.form);
      // 校验表单
      await this.$refs.form.validate();

      // 修改加载框为正在加载
      this.loading = true;

      let result = null;

      // 执行编辑或修改
      if (this.isUpdate) {
        result = VehicleApi.edit(this.form);
      } else {
        result = VehicleApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加载框
          this.loading = false;

          // 提示添加成功
          message.success(result.message);

          // 如果是新增，则form表单置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);

          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author cancan
     * @date 2023/10/03 23:15
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
};
</script>
